package com.all.play.mypsm.shoppingtcart.utils;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.util.SparseArray;

import com.all.play.mypsm.App.MyApplication;
import com.all.play.mypsm.home.bean.GoodsInfo;
import com.all.play.mypsm.utils.CacheUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 聪明一只哈 on 2018/4/2.
 * 利用此类将数据进行增删改查,进内存中,同时保存到本地
 */

public class CartSorage {

    private static final String JSON_CART = "json_cart";
    private static CartSorage cartSorage;
    private Context context;
    private final SparseArray<GoodsInfo> sparseArray;

    private CartSorage(Context context) {
        this.context = context;
        //内存是hashimap ,但是性能比hashimap优越
        sparseArray = new SparseArray<>(100);

        //本地数据转换成SparseArray
        listToSpareArray();

    }


    public static CartSorage getInstance() {
        if (cartSorage == null) {
            cartSorage = new CartSorage(MyApplication.getContext());
        }
        return cartSorage;
    }

    /**
     * 集合转SpareArray
     *
     * @author 聪明一只哈
     * @time 2018/4/2 9:50
     */
    private void listToSpareArray() {
        List<GoodsInfo> list = getAllData();
        Log.i("savelocalte", "saveLocal: "+list.size());
        for (int i = 0; i < list.size(); i++) {
            sparseArray.put(Integer.parseInt(list.get(i).getProduct_id()), list.get(i));
        }

    }

    /**
     * 从本地读取的数据加入到SparseArray中
     */
    public List<GoodsInfo> getAllData() {
        List<GoodsInfo> list = new ArrayList<>();

        //获取本地的数据json
        String json_cart = CacheUtils.getString(context, JSON_CART);

        //json转集合
        if (!TextUtils.isEmpty(json_cart)) {
            list = new Gson().fromJson(json_cart, new TypeToken<List<GoodsInfo>>() {
            }.getType());
        }


        return list;
    }

    /**
     * 添加数据,并本地同步
     *
     * @author 聪明一只哈
     * @time 2018/4/2 9:59
     */
    public void addData(GoodsInfo goodsInfo) {
        GoodsInfo tempgoods = sparseArray.get(Integer.parseInt(goodsInfo.getProduct_id()));
        if(tempgoods!=null){//说明内存中sparseArray,已经有值
            //则执行此方法,数量加1
            tempgoods.setNumber(tempgoods.getNumber()+1);
        }else {
            /*//内存中没有此数据,则添加到内存中
            sparseArray.put(Integer.parseInt(tempgoods.getPreduct_id()),tempgoods);*/
            tempgoods=goodsInfo;
            tempgoods.setNumber(1);
        }
        //内存中没有此数据,则添加到内存中
        sparseArray.put(Integer.parseInt(tempgoods.getProduct_id()),tempgoods);
        
        //同时,保存到本地
        saveLocal();
    }
    
    /**
     *删除数据
     *@author 聪明一只哈
     *@time 2018/4/2 10:16
     */
    public void deleteData(GoodsInfo goodsInfo){
        //1.内存中删除
        sparseArray.delete(Integer.parseInt(goodsInfo.getProduct_id()));
        //2.同步到本地
        saveLocal();
    }
    
    /**
     *
     *@author 聪明一只哈
     *@time 2018/4/2 10:16
     */
    public void updateData(GoodsInfo goosinfo){

        //1.更新内存的数据
        sparseArray.put(Integer.parseInt(goosinfo.getProduct_id()),goosinfo);
        //2.同步到本地
        saveLocal();

    }

    

    /**
     *保存到本地
     *@author 聪明一只哈
     *@time 2018/4/2 10:14
     */
    private void saveLocal() {
        //1.在内存中,转换成list
        List<GoodsInfo> goodinfolist=getSparseArrayToList();
        //2.用Gson 将list装换成String
        String json = new Gson().toJson(goodinfolist);
        Log.i("savelocalte", "saveLocal: "+json);
        //3.sp存储
        CacheUtils.saveString(context,JSON_CART,json);
        
    }

    //转换
    private List<GoodsInfo> getSparseArrayToList() {

        List<GoodsInfo> goodslist=new ArrayList<>();
        for (int i=0;i<sparseArray.size();i++){

            goodslist.add(sparseArray.valueAt(i));

        }

        return goodslist;
    }


}
